#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project : gtmsAutoTestProject 
@File    : base_pos_api.py
@IDE     : PyCharm 
@Author  : Mario
@Date    : 2022/4/25 16:48 
@Describe: POS系统接口请求基类
"""
from APIs.base_api import BaseApi
from Config.yamlRead import sys_cfg
from Comm.tools.toolsControl import ToolsHandler


class BasePosApi(BaseApi):
    def __init__(self, brandCode: str, environment: str):
        """
        构造函数
        :param brandCode: 品牌编码
        :param environment: 环境
        """
        super().__init__()  # 继承父类构造函数
        self.type = "POS"
        self.url = sys_cfg['posApiUrl'][environment]  # 取出配置文件URL前缀
        credentials_sql = "SELECT token,device_mac,account,password FROM login_credentials WHERE environment = '{}' AND brand_code = '{}'".format(environment, brandCode)
        credentials_result = self.sqlite.find_data(credentials_sql)[0]
        self.token = credentials_result[0]  # 取出sqlite中POS请求token
        # 根据品牌取出设备MAC
        self.loginMac = credentials_result[1]  # 取出sqlite中POS请求mac
        # 取出POS登录账号密码
        self.shortNo = credentials_result[2]  # 取出sqlite中POS请求shortNo
        self.password = credentials_result[3]  # 取出sqlite中POS请求password

    def get_headers(self) -> object:
        """
        获取POS请求头
        :return:
        """
        timestamp = ToolsHandler.get_timestamp()
        headers = {
            'Connection': 'keep-alive',
            'retry_count': '1',
            'x-http-timestamp': timestamp,
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) ??POS?????/1.0.22 Chrome/87.0.4280.60 Electron/11.0.1 Safari/537.36',
            'retry': '4',
            'Accept': '*/*',
            'x-http-token': self.token,
            'x-http-osversion': '1.0.22',
            'x-http-request-id': self.loginMac + '-' + timestamp,
            'Accept-Language': 'zh-CN',
            'Cookie': 'SERVERID=k8s-ingress.gtmsh.com:10080; x-http-token=' + self.token,
            'Content-Type': 'application/json'
        }
        return headers


if __name__ == '__main__':
    test = BasePosApi('823882', 'uat')
    url = 'table/list'
    payload = '{"operate":"0"}'
    test.http_requests(url, payload)
